package com.youlu.campus.admin.complaint;

import com.youlu.campus.service.mail.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class ComplaintTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MailService mailService;

    /**
     * 每小时执行一次
     * 每天早上9：00~23:00触发
     */
    @Scheduled(cron = "0 0 9-23 * * ?")
    public void sendComplaintMailJob(){
        Boolean flag = redisTemplate.opsForValue().setIfAbsent("complaint:mail", "1", 3L, TimeUnit.MINUTES);
        if(Objects.nonNull(flag)&&!flag){
            return;
        }
        mailService.sendMail();
    }


}
